package org.oims.simpleserver.session;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class HttpSessionHub {
    private Map<String, HttpSession> idSessionMap;

    public HttpSessionHub(){
        this.idSessionMap = new ConcurrentHashMap<>();
    }

    public void put(String id, HttpSession session){
        HttpSession s = idSessionMap.get(id);
        if(s != null){
            idSessionMap.remove(id);
            Enumeration<String> names = s.getAttributeNames();
            while (names.hasMoreElements()){
                String name = names.nextElement();
                s.removeAttribute(name);
            }
        }
        idSessionMap.put(id, session);
    }

    public void remove(String id){
        idSessionMap.remove(id);
    }
}
